﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace FiyatDegisikligi
{
    public partial class frmEtiketTanim : XtraForm
    {
        DataTable dt = new DataTable();

        public frmEtiketTanim()
        {
            InitializeComponent();
        }

        private void frmEtiketTanim_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("Dizayn Adı", typeof(string));
            dt.Columns.Add("Yazıcı Yolu", typeof(string));
            dt.Columns.Add("Ean18 Dosya Adi", typeof(string));
            dt.Columns.Add("Ean8 Dosya Adi", typeof(string));

            if (File.Exists(Application.StartupPath + "\\etiket_dizaynlari.txt"))
            {
                using (StreamReader sr = new StreamReader(Application.StartupPath + "\\etiket_dizaynlari.txt"))
                {
                    string line = string.Empty;
                    while (sr.Peek() > -1)
                    {
                        line = sr.ReadLine();
                        string[] alan = line.Split(';');
                        dt.Rows.Add(alan[0], alan[1], alan[2], alan[3]);
                    }
                    sr.Close();
                }
            }
            gridControl1.DataSource = dt;
        }

        private void dosyayaYaz()
        {
            using (StreamWriter sw = new StreamWriter(Application.StartupPath + "\\etiket_dizaynlari.txt", false))
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    sw.WriteLine(string.Format("{0};{1};{2};{3}", gridView1.GetRowCellDisplayText(i, gridView1.Columns[0]).ToString(), gridView1.GetRowCellDisplayText(i, gridView1.Columns[1]).ToString(), gridView1.GetRowCellDisplayText(i, gridView1.Columns[2]).ToString(), gridView1.GetRowCellDisplayText(i, gridView1.Columns[3]).ToString()));
                }
                sw.Close();
            }
        }

        private void btnEkle_Click(object sender, EventArgs e)
        {
            dt.Rows.Add(txtDizaynAdi.Text, txtYaziciAdi.Text, txtEan13.Text, txtEan8.Text);
            dosyayaYaz();
        }

        private void btnSil_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                dt.Rows.RemoveAt(gridView1.FocusedRowHandle);
                txtDizaynAdi.Text = "";
                txtYaziciAdi.Text = "";
                txtEan13.Text = "";
                txtEan8.Text = "";
                dosyayaYaz();
            }
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                txtDizaynAdi.Text = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, gridView1.Columns[0]).ToString();
                txtYaziciAdi.Text = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, gridView1.Columns[1]).ToString();
                txtEan13.Text = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, gridView1.Columns[2]).ToString();
                txtEan8.Text = gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, gridView1.Columns[3]).ToString();
            }
        }
    }
}
